<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

/*
|--------------------------------------------------------------------------
| AIR2 Source search page
|--------------------------------------------------------------------------
*/
$search_url = $c->uri_for('search.json', array('gzip' => $use_gzip, 'i'=>$search_idx));
$source_url = $c->uri_for('source');
?>
<script type="text/javascript">
    Ext.onReady(function() {

        AIR2.Search.URL     = '<?php echo $c->uri_for("search") ?>';
        AIR2.Search.QUERY   = '<?php echo htmlspecialchars($q, ENT_QUOTES) ?>';
        AIR2.Search.PARAMS  = <?php echo Encoding::json_encode_utf8($params) ?>;
        AIR2.Search.IDX     = '<?php echo $search_idx ?>';

        var resultTpl = new Ext.XTemplate(
           '<tpl for=".">',
            '<div class="air2-search-result">',
             '<table>',
             '<tr>',
             '<td>',
             '<div class="source-contact">',
              '<h3>',
               '<span>{rank}.</span> <a href="<?php echo $source_url ?>/{uri}">{title}</a>',
               '<a href="<?php echo $source_url ?>/{uri}" title="View {response_sets_count} responses and contact summary" ',
                ' onclick="AIR2.Search.Source.Details(this,&quot;{[values.rank-1]}&quot;); return false;" ',
                ' class="air2-icon-responses source-expand">&nbsp;</a>',
              '</h3>',
              '<ul>',
               '<li class="r-loc">{primary_location}</li>',
               '<li class="r-phone">{primary_phone}</li>',
               '<li class="r-mail last"><a href="mailto:{primary_email}">{primary_email_html}</a></li>', // TODO air2 mta?
              '</ul>',
             '</div>', // source-contact
             '</td>',
             '<td>',
             '<div class="r-excerpt">',
              '<div class="r-activity">',
               '<span class="r-last">Last inquiry: {[AIR2.Format.dateYmd(values.last_contacted_date)]}</span>',
               '<span class="r-last">Last response: {[AIR2.Format.dateYmd(values.last_response_date)]}</span>',
              '</div>',
              '<tpl if="summary.length">',
               '<div class="r-snip"><strong>Response:</strong><span class="snip">&nbsp;{summary}</span></div>',
              '</tpl>',
              '<tpl for="excerpts">',
               '<div class="r-snip">',
                '<strong>{[AIR2.Fixtures.FieldLabels[values.field]||values.field]}:</strong>&nbsp;',
                '<span class="snip">{snip}</span>',
               '</div>',
              '</tpl>',
             '</div>', // r-excerpt
             '</td>',
             '</tr>',
             '</table>',
            '</div>',  // air2-search-result
           '</tpl>'
        );
        resultTpl.compile();

        // TODO use labels from AIR2.Fixtures
        var facetDefs = {
            user_gender : {
                label : 'Gender'
            },
            smadd_cntry : {
                label : 'Country',
                itemLabels: AIR2.Fixtures.Countries
            },
            smadd_state : {
                label : 'State/Province',
                itemLabels: AIR2.Fixtures.States
            },
            src_household_income : {
                label : 'Household Income'
            },
            org_uuid : {
                label : 'Organization',
                itemLabels : <?php echo Encoding::json_encode_utf8($org_names) ?>
            },
            user_religion : {
                label : 'Religion'
            },
            last_activity_date : {
                label : 'Last Activity'
            },
            last_response_date : {
                label : 'Last Response'
            },
            user_ethnicity : {
                label : 'Ethnicity'
            },
            birth_year : {
                label : 'Age'
            },
            tag : {
                label : 'Tag'
            },
       /* this one is too broad to be helpful, because it tracks
          all the inquiries any of the sources has responded to,
          not just the ones that match this query

            inq_uuid_title : {
                label : 'Inquiry'
            },
        */
            prj_uuid_title : {
                label : 'Project'
            }


        };

        var app = new AIR2.UI.App({
            items: AIR2.SearchPanel({
                title       : '<?php echo $search_label ?>',
                searchUrl   : '<?php echo $search_url ?>',
                resultTpl   : resultTpl,
                facetDefs   : facetDefs
            })
        });

    });
</script>
